using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebAppCore.Ext;
using WebAppCore.TagHelpers;

namespace WebApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services
                    .AddControllersWithViews(options =>
                    {
                        options.EnableEndpointRouting = false;
                        // 自定义 BinderProvider
                        options.ModelBinderProviders.Insert(0, new AuthorEntityBinderProvider());
                    })
                    .ConfigureApplicationPartManager(apm =>
                    {
                        // 自定义 ControllerFeature
                        apm.FeatureProviders.Add(new GenericControllerFeatureProvider());
                    });


            // Swagger
            services.AddSwaggerGen(config =>
            {
                config.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
            });


            // Custom TagHelper
            services.AddTransient<ITagHelperComponent, AddressTagHelperComponent>();

            /* NoUsed 

            // SessionStateTempDataProvider or CookieTempDataProvider
            .AddSessionStateTempDataProvider() 
            // MVC Compatibility
            .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest)
            // ModelBind XML
            .AddXmlSerializerFormatters();  

            services.AddDistributedMemoryCache();

            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(10);
                options.Cookie.HttpOnly = true;
            });

            services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            */

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

            // In the Configure method, insert middleware to expose the generated Swagger as JSON endpoint(s)
            // At this point, you can spin up your application and view the generated Swagger JSON at "/swagger/v1/swagger.json."
            app.UseSwagger();

            // Optionally, insert the swagger - ui middleware if you want to expose interactive documentation
            // specifying the Swagger JSON endpoint(s) to power it from.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });
        }
    }
}
